package com.sq.shortlink.admin.controller;

import com.sq.shortlink.admin.dto.req.UserLoginReqDTO;
import com.sq.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.sq.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.sq.shortlink.admin.dto.resp.UserActualRepDTO;
import com.sq.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.sq.shortlink.admin.service.UserService;
import com.sq.shortlink.admin.common.convension.result.Result;
import com.sq.shortlink.admin.common.convension.result.Results;
import com.sq.shortlink.admin.dto.resp.UserRepDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制层
 * @author quan
 */
@RestController
@RequestMapping("/api/short-link/admin/v1/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查询未脱敏用户信息
     */
    @GetMapping("/actual/{username}")
    public Result<UserActualRepDTO> getActualUserByUsername(@PathVariable String username) {
        return Results.success(userService.getActualUserByUsername(username));
    }

    /**
     * 根据用户名查询脱敏用户信息
     */
    @GetMapping("/{username}")
    public Result<UserRepDTO> getUserByUsername(@PathVariable String username) {
        return Results.success(userService.getUserByUsername(username));
    }

    /**
     * 查询用户名是否可用
     */
    @GetMapping("/has-username")
    public Result<Boolean> hasUsername(String username) {
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 用户注册
     */
    @PostMapping
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }

    /**
     * 用户修改信息
     */
    @PutMapping
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam){
        userService.update(requestParam);
        return Results.success();
    }


    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        return Results.success(userService.login(requestParam));
    }

    /**
     * 检查用户是否已登录
     */
    @GetMapping("/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username,@RequestParam("token") String token){
        return Results.success(userService.checkLogin(username,token));
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }


}
